﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TimeSplicerModule.Services.DeveloperServices;

namespace TimeSplicerModule.Views
{
    /// <summary>
    /// Interaction logic for ServicesView.xaml
    /// </summary>
    public partial class ServicesView : UserControl
    {
        private readonly ISchedulingService _schedulingService;
        private Timer _getAutoTasksTimer;
        private Timer _startAutoTasksTimer;
        private Timer _createInstancesTimer;

        public ServicesView(ISchedulingService schedulingService)
        {
            InitializeComponent();

            _schedulingService = schedulingService;
        }

        // need to create some tasks specifically for testing

        private void StartService()
        {
            // call each before timers are started
            _schedulingService.StoreAutoTasksForTheDay();
            _schedulingService.CreateTaskInstances();
            _schedulingService.StartAutoTasks();


            _getAutoTasksTimer = new Timer(TimeSpan.FromDays(1).TotalMilliseconds);
            _getAutoTasksTimer.Elapsed += _timer_getAutoTasks_elapsed;
            _getAutoTasksTimer.AutoReset = false;
            _getAutoTasksTimer.Start();

            _startAutoTasksTimer = new Timer(TimeSpan.FromMinutes(1).TotalMilliseconds);
            _startAutoTasksTimer.Elapsed += _timer_startAutoTasks_elapsed;
            _startAutoTasksTimer.AutoReset = false;
            _startAutoTasksTimer.Start();

            _createInstancesTimer = new Timer(TimeSpan.FromHours(1).TotalMilliseconds);
            _createInstancesTimer.Elapsed += _timer_createInstances_elapsed;
            _createInstancesTimer.AutoReset = false;
            _createInstancesTimer.Start();
        }

        private void StopService()
        {
            if (_getAutoTasksTimer != null)
            {
                _getAutoTasksTimer.Stop();
                _getAutoTasksTimer.Dispose();
                _getAutoTasksTimer = null;
            }

            if (_startAutoTasksTimer != null)
            {
                _startAutoTasksTimer.Stop();
                _startAutoTasksTimer.Dispose();
                _startAutoTasksTimer = null;
            }

            if (_createInstancesTimer != null)
            {
                _createInstancesTimer.Stop();
                _createInstancesTimer.Dispose();
                _createInstancesTimer = null;
            }
        }

        private void taskScheduleButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.taskScheduleButton.Content.ToString() == "START")
            {
                StartService();

                this.taskScheduleButton.Content = "STOP";
                this.taskSchedulerMessage.Text = "Running";
            }
            else if (this.taskScheduleButton.Content.ToString() == "STOP")
            {
                StopService();

                this.taskScheduleButton.Content = "START";
                this.taskSchedulerMessage.Text = "Stopped";
            }
        }

        private void _timer_getAutoTasks_elapsed(object sender, ElapsedEventArgs e)
        {
            Timer timer = sender as Timer;

            _schedulingService.StoreAutoTasksForTheDay();
            timer.Enabled = true;
        }

        private void _timer_startAutoTasks_elapsed(object sender, ElapsedEventArgs e)
        {
            Timer timer = sender as Timer;

            _schedulingService.StartAutoTasks();
            timer.Enabled = true;
        }

        private void _timer_createInstances_elapsed(object sender, ElapsedEventArgs e)
        {
            Timer timer = sender as Timer;

            _schedulingService.CreateTaskInstances();
            timer.Enabled = true;
        }
    }
}
